<?php
namespace Admin\Service;

use Think\Service\BasicService;

class CostService extends BasicService {
    public function insert($model) {
        return $this->db->add($model);
    }

    public function Add($model) {
        return $this->db->add($model);
    }

    public function AddAll($model) {
        $data = $this->db->addAll($model);
        return $data;
    }

    public function remove($condition) {
        return $this->db->where($condition)->delete();
    }

    public function Del($condition) {
        return $this->db->where($condition)->delete();
    }

    public function update($where, $model = array()) {
        return $this->db->where($where)->save($model);
    }

    public function find($condition, $field = '*') {
        return $this->db->where($condition)->field($field)->find();
    }

    public function query($condition, $limit = array(), $field = '*', $order = null, $group = null) {
        $data = $this->db->field($field)->where($condition)->limit($limit['start'], $limit['end'])->order($order)->group($group)->select();
        return $data;
    }

    /*
     *
     * 并表1个
     */
    public function join($condition, $join = '', $field = '*', $limit = array(), $order = null, $group = null) {
        $data = $this->db->join($join)->field($field)->where($condition)->limit($limit['start'], $limit['end'])->order($order)->group($group)->select();
        return $data;
    }

    public function count($condition) {
        return $this->db->where($condition)->count();
    }

    /*
     * 用户余额增加
     * $user_id 用户id
     * $money  增加金额、
     * $order_id 订单id
     */
    public function UserCostAdd($user_id, $money, $order_id = null){
        $this->db->startTrans();
        $where['USER_ID'] = $user_id;
        $costData = $this->db->where($where)->find();
        $costStr =  passport_encrypt((string)((float)passport_decrypt($costData['MONEY'],C('JM_KEY')) +$money),C('JM_KEY'));
        $csave['MONEY'] = $costStr;
        $csave['UPDATE_TIME'] = date('Y-m-d H:i:s');
        $csave['UPDATE_IP'] = get_client_ip();
        $C_OK = $this->db->where($where)->save($csave);
        if($C_OK){
            //生成交易记录
            $CL_LS = M('cost_log');
            $clsave['C_ID'] = 'CL_' . uniqid() . rand(0, 99);
            $clsave['OID'] = $order_id;
            $clsave['USER_ID'] = $user_id;
            $clsave['PAY_TYPE'] = 3;
            $clsave['MONEY'] = $money;
            $clsave['MONEY_TYPE'] = 2;
            $clsave['SR_TYPE'] = 2;
            $clsave['CREATE_TIME'] = date('Y-m-d H:i:s');
            $clsave['STATE'] = 1;
            $clsave['INFO'] = '用户卖书，增加平台余额';
            $CL_OK = $CL_LS->Add($clsave);
            if($CL_OK){
                $this->db->commit();
                return 1;
            }
        }
        $this->db->rollback();
        return 0;

    }

    /*
     * 超级管理员修改用户余额
     */
    public function UpdateCost($user_id, $money, $order_id = null){
        $this->db->startTrans();
        $where['USER_ID'] = $user_id;
        $costData = $this->db->where($where)->find();
        $costStr =  passport_encrypt((string)((float)passport_decrypt($costData['MONEY'],C('JM_KEY')) +$money),C('JM_KEY'));
        $csave['MONEY'] = $costStr;
        $csave['UPDATE_TIME'] = date('Y-m-d H:i:s');
        $csave['UPDATE_IP'] = get_client_ip();
        $C_OK = $this->db->where($where)->save($csave);
        if($C_OK){
            //生成交易记录
            $CL_LS = M('cost_log');
            $clsave['C_ID'] = 'CL_' . uniqid() . rand(0, 99);
            $clsave['OID'] = $order_id;
            $clsave['USER_ID'] = $user_id;
            $clsave['PAY_TYPE'] = 3;
            $clsave['MONEY'] = $money;
            $clsave['MONEY_TYPE'] = 5;
            $money>0?$clsave['SR_TYPE'] = 2:$clsave['SR_TYPE'] = 1;
            $clsave['CREATE_TIME'] = date('Y-m-d H:i:s');
            $clsave['STATE'] = 1;
            $clsave['INFO'] = '超级管理员直接修改了用户余额，修改金额:'.$money;
            $CL_OK = $CL_LS->Add($clsave);
            if($CL_OK){
                $this->db->commit();
                return 1;
            }
        }
        $this->db->rollback();
        return 0;

    }

}

?>